package com.wondersgroup.aida.tools;

import com.google.common.collect.Lists;
import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.agent.tool.ToolSpecifications;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ToolExecutionResultMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.output.Response;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Collections;

/**
 * @author: 紫麒麟vip
 * @create: 2025/2/15
 * Description:
 */
public class Demo1 {

    @Tool("获取当前日期")
    public static String dataUtil(){
        return LocalDateTime.now().toString();
    }
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ChatLanguageModel model = ModelUtil.getOpenAIMOdel();

        ToolSpecification toolSpecification = ToolSpecifications.toolSpecificationFrom(Demo1.class.getMethod("dataUtil", null));

        UserMessage userMessage = UserMessage.from("今天是几月几号？");

        Response<AiMessage> response = model.generate(Collections.singletonList(userMessage), toolSpecification);

        AiMessage aiMessage = response.content();
        System.out.println(aiMessage);

        if (aiMessage.hasToolExecutionRequests()) {
            for (ToolExecutionRequest toolExecutionRequest : aiMessage.toolExecutionRequests()) {
                String methodName = toolSpecification.name();
                Method method = Demo1.class.getMethod(methodName);
                String result = (String) method.invoke(null);
                System.out.println(result);

                ToolExecutionResultMessage toolExecutionResultMessage = ToolExecutionResultMessage.from(toolExecutionRequest.id(), toolExecutionRequest.name(), result);
                Response<AiMessage> res = model.generate(Lists.newArrayList(userMessage, aiMessage, toolExecutionResultMessage));
                System.out.println(res.content().text());
            }
        }
    }
}
